<?php
/*  LOAD SYSTEM FUNCTIONS MANUALLY
    ============================================================================================================== */
	require_once( "functions/inc.database.php" );
	require_once( "functions/inc.getData.php" );
	require_once( "functions/inc.pageSetup.php" );
	
	# init session
	session_start();
	
	page_header( "Wayne Schlegel Inc. | Password Recovery", false );
	
?>
	<?php
	if( empty( $_POST['username'] ) ) {
	?>
	<h1>Forgot your password?</h1>
	<p>Please give us your username, and we will send your password to the e-mail-address you gave us during your registration.</p>
	<form sction="" method="post" class="iForm formTable">
		<label for="username">username</label>
		<input type="text" name="username" />
		<label>&nbsp;</label>
		<input type="submit" name="send" value="get password" />
	</form>
	<?php
	} else {
	// find and send password
		$sql = "SELECT * FROM userData where username = '" . $_POST['username'] . "'";
		$res = mysql_query( $sql );
		if( mysql_num_rows( $res ) == 0 ){
			// noch such username found
			echo "There's no account with this username. Please <a href=\"loginRegister.php\">register now</a>.";
		} else {
			// get detailed contactData
			$row = mysql_fetch_array( $res );
			$thisUser = getUserData( $row['id'] );
			$mailtext  = "Dear " . $thisUser['firstName'] . " "  . $thisUser['lastName'] . ",\n";
			$mailtext .= "the password for your account has been requested. Please login at http://" . $_SERVER['SERVER_NAME'] . " with your password >>> " . $thisUser['password'] . " <<<.\n";
			$mailtext .= "Because of security reasons we recommend you to change your password in your profile.\n\n";
			$mailtext .= "With kind regards,\n your Wayne Schlegel Inc. Team";
			$headers = 'From: registration@wayne-schlegel.com' . "\r\n" . 'Reply-To: cmd@stefanmoeller.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
			if( mail( $thisUser['email'], "[Wayne Schlegel Inc.] Password recovery on your account.", $mailtext, $headers ) ){
				echo "<p>Your password was send to the e-mail-address you gave us during your registration. Please check your mail-account.</p>";
			} else {
				echo "<p>Sorry. An error occured while sending your password. Please try again later or contact our support-team.</p>";
			}
		}
	}
	?>
	
	
	<table class="iTable">
		<tr class="moreInformation lastItem">
			<td>
				<a href="loginForm.php">back to Login</a>
			</td>
		</tr>
	</table>
</body>
</html>